<?php
// +----------------------------------------------------------------------
// | ranking [ 捐款平台公款系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://localhost.ranking.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 七秒の记忆 <Majw998@Gmail.com>
// +----------------------------------------------------------------------
namespace Home\Model;
use Think\Model;

class ArticleModel extends Model{
	protected $_validate = array(
		array('title', 'require', '标题不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('content', 'require', '内容不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
	);

	protected $_auto = array(
		array('create_time', NOW_TIME, self::MODEL_INSERT),
		array('update_time', NOW_TIME, self::MODEL_BOTH),
	);

	public function update($data) {
		$info       = array();
		$info['e']  = 1;

		if(!$this->create($data)) {
			$info['e']    = -1;
			$info['info'] = $this->getError();
			return $info;
		}

		if($data['id']) {
			$info['info'] = $this->save();
		} else {
			$info['info'] = $this->add();
		}
		return $info;
	}

	public function _lists($keyword='', $p=1, $sort='create_time DESC') {
		$map['is_delete'] = 1;
		if($keyword) { $map['title']  = array('like', '%' . $keyword . '%'); };

		$count 			= $this->where($map)->count();
		$page  			= new \Think\Page($count,C('INFO_CONFIG.P'));
		$list['lists']  = $this->where(array($map))->order($sort)->limit($page->firstRow.','.$page->listRows)->select();
		$list['show'] 	= $page->show();
		return $list;
	}

}